-include common.mk

#
# Input arguments:
#	$(BOARDPATH)	- Board path
#	$(KCONFIG_TOOL)	- Kconfig tool
#	$(PYTHON_TOOL)	- Python tool
#

# -------- parameters checkout

ifeq ($(BOARDPATH),)
$(error BOARDPATH should not be null)
endif
ifeq ($(KCONFIG_TOOL),)
$(error KCONFIG_TOOL should not be null)
endif
ifeq ($(PYTHON_TOOL),)
$(error PYTHON_TOOL should not be null)
endif

# -------- variables

# mxsetup.h and .config
CONFIG			:= $(BOARDPATH)/mxsetup.h
KCONFIG			:= $(BOARDPATH)/.config
# default config folder name
DEFCONFIG		?= $(BOARDPATH)/defconfig

# for Kconfig tool to modify .config path
export KCONFIG_CONFIG	:= $(KCONFIG)

# -------- kernel rules

# main rule
$(BOARDPATH)/build/$(TARGET).elf : $(CONFIG)
	@$(MAKE) -f meta.mk
	@$(MAKE) -f compile.mk
$(CONFIG) : $(KCONFIG)
	@$(PYTHON_TOOL) ./tools/kconfig/generate.py $< $@
$(KCONFIG):
	@$(KCONFIG_TOOL) Kconfig

# default .config rule
%_defconfig:
	@cp $(DEFCONFIG)/$@ $(KCONFIG)
	@$(PYTHON_TOOL) ./tools/kconfig/generate.py $(KCONFIG) $(CONFIG)
	@$(MAKE) -f meta.mk clean
	@$(MAKE) -f meta.mk
	@rm -rf $(BOARDPATH)/build/*

# -------- kernel process rules

kapp:
	@$(MAKE) -f kprocess.mk

kapp-clean:
	@$(MAKE) -f kprocess.mk clean
	@echo clean up completed.

# -------- pseudo target

.PHONY: menuconfig clean distclean help

menuconfig:
	@$(KCONFIG_TOOL) Kconfig
	@$(PYTHON_TOOL) ./tools/kconfig/generate.py $(KCONFIG) $(CONFIG)
	@$(MAKE) -f meta.mk clean
	@$(MAKE) -f meta.mk
	@rm -rf $(BOARDPATH)/build/*

metaconfig:
	@$(MAKE) -f meta.mk

clean:
	@$(MAKE) -f compile.mk clean
	@echo clean up completed.

distclean:
	@$(MAKE) -f compile.mk clean
	@$(MAKE) -f meta.mk clean
	@rm -rf $(KCONFIG) $(CONFIG)
	@echo distclean up completed.

help:
	$(call HELP_MSG," menuconfig : Open mconf UI.")
	$(call HELP_MSG," metaconfig : Generate META config files.")
	$(call HELP_MSG," clean 	 : Clean the target exclude config files.")
	$(call HELP_MSG," distclean  : Clean the target include config files.")
	$(call HELP_MSG," V 		 : Get the detail information of compile if V=1 in commandline.")
	$(call HELP_MSG,"-j 		 : multicore compilation.")
